import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);
const router = new VueRouter({
    mode: 'history', base: process.env.BASE_URL, routes: [
        {
            path: "/admin",
            component: () => import("../views/dashboard.vue"),
            redirect: "/",
            children: [
                {
                    path: "/",
                    component: () => import("../views/index/layout.vue"),
                    redirect: "dashboard",
                    children: [
                        {
                            path: "dashboard",
                            component: () => import("../views/index/index/index.vue"),
                        },
                        {
                            path: "backstage/account",
                            component: () => import("../views/index/backStage/account/index.vue"),
                            meta: {breadCrumb: ["后台管理", "运营账号管理"]}
                        },
                        {
                            path: "backstage/group",
                            component: () => import("../views/index/backStage/group/index.vue"),
                            meta: {breadCrumb: ["后台管理", "权限组管理"]}
                        },
                        {
                            path: "backstage/power",
                            component: () => import("../views/index/backStage/power/index.vue"),
                            meta: {breadCrumb: ["后台管理", "权限管理"]}
                        },
                        {
                            path: "system/dictionary",
                            component: () => import("../views/index/system/dictionary/index.vue"),
                            meta: {breadCrumb: ["系统设置", "字典管理"]}
                        },
                        {
                            path: "operator",
                            component: () => import("../views/index/operator/index.vue"),
                            meta: {breadCrumb: ["运营方管理"]}
                        },
                    ]
                },
                {
                    path: "login",
                    component: () => import("../views/login/login.vue"),
                },
            ]
        }
    ]
})


router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('adminToken');
    if (to.meta && to.meta.title) {
        document.title = to.meta.title;
    }
    if (to.path !== '/admin/login' && !token) {
        next('/admin/login');
    } else {
        next();
    }
});
const routerData = localStorage.getItem("routers");
// 如果routerData存在且有效，添加动态路由
if (routerData) {
    const parsedRouterData = JSON.parse(routerData);
    parsedRouterData.forEach(route => {
        if (route.component) {
            router.addRoute(route);
        }
    });
}

// 重写push和replace方法
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};

export default router;
